package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AppDynamicMsg;
import com.ruoyi.system.domain.AppDynamicTimeline;
import com.ruoyi.system.domain.AppUserRelation;
import com.ruoyi.system.mapper.AppDynamicMsgMapper;
import com.ruoyi.system.service.IAppDynamicMsgService;
import com.ruoyi.system.service.IAppDynamicTimelineService;
import com.ruoyi.system.service.IAppUserRelationService;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AppDynamicMapper;
import com.ruoyi.system.domain.AppDynamic;
import com.ruoyi.system.service.IAppDynamicService;

/**
 * 动态Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-27
 */
@Service
public class AppDynamicServiceImpl extends ServiceImpl<AppDynamicMapper, AppDynamic> implements IAppDynamicService {
    @Autowired
    private AppDynamicMapper appDynamicMapper;
    @Autowired
    private IAppDynamicTimelineService iAppDynamicTimelineService;
    @Autowired
    private IAppDynamicMsgService iAppDynamicMsgService;
    @Autowired
    private IAppUserRelationService iAppUserRelationService;

    /**
     * 查询动态
     *
     * @param id 动态主键
     * @return 动态
     */
    @Override
    public AppDynamic selectAppDynamicById(Long id) {
        return appDynamicMapper.selectAppDynamicById(id);
    }

    /**
     * 查询动态列表
     *
     * @param appDynamic 动态
     * @return 动态
     */
    @Override
    public List<AppDynamic> selectAppDynamicList(AppDynamic appDynamic) {
        return appDynamicMapper.selectAppDynamicList(appDynamic);
    }

    /**
     * 新增动态
     *
     * @param appDynamic 动态
     * @return 结果
     */
    @Override
    public int insertAppDynamic(AppDynamic appDynamic) {
        return appDynamicMapper.insertAppDynamic(appDynamic);
    }

    /**
     * 修改动态
     *
     * @param appDynamic 动态
     * @return 结果
     */
    @Override
    public int updateAppDynamic(AppDynamic appDynamic) {
        return appDynamicMapper.updateAppDynamic(appDynamic);
    }

    /**
     * 批量删除动态
     *
     * @param ids 需要删除的动态主键
     * @return 结果
     */
    @Override
    public int deleteAppDynamicByIds(Long[] ids) {
        return appDynamicMapper.deleteAppDynamicByIds(ids);
    }

    /**
     * 删除动态信息
     *
     * @param id 动态主键
     * @return 结果
     */
    @Override
    public int deleteAppDynamicById(Long id) {
        return appDynamicMapper.deleteAppDynamicById(id);
    }

    @Override
    public AjaxResult releaseDynamic(AppDynamic dynamicEntity) {
        // 保存动态
        appDynamicMapper.insertAppDynamic(dynamicEntity);

        // 添加自己的时间轴
        AppDynamicTimeline selfTimeline = new AppDynamicTimeline();
        selfTimeline.setCrtTime(dynamicEntity.getCrtTime());
        selfTimeline.setUserId(dynamicEntity.getUserId());
        selfTimeline.setDynamicId(dynamicEntity.getId());
        selfTimeline.setIsOwn(1);
        selfTimeline.setIsRead(1);
        iAppDynamicTimelineService.insertAppDynamicTimeline(selfTimeline);

        // 提醒用户插入消息
        if (StringUtils.isNotBlank(dynamicEntity.getRemindUserIds())) {
            List<String> remindUserIdList = Arrays.asList(dynamicEntity.getRemindUserIds().split(","));
            AppDynamicMsg msgEntity;
            String dynamicImg = "";
            for (String remindUserId : remindUserIdList) {
                msgEntity = new AppDynamicMsg();
                msgEntity.setCrtTime(dynamicEntity.getCrtTime());
                msgEntity.setUserId(Long.parseLong(remindUserId));
                msgEntity.setDynamicId(dynamicEntity.getId());
                msgEntity.setDynamicType(dynamicEntity.getType());
                msgEntity.setDynamicContent(dynamicEntity.getUserName() + "发表了一条动态：" + dynamicEntity.getContent());
                if (dynamicEntity.getType() == 1)
                    dynamicImg = JSONArray.parseArray(dynamicEntity.getImgs()).getString(0);
                else if (dynamicEntity.getType() == 2) dynamicImg = dynamicEntity.getVideoCover();
                msgEntity.setDynamicImg(dynamicImg);
                msgEntity.setMsgType(3);
                msgEntity.setFromUserId(dynamicEntity.getUserId());
                msgEntity.setFromUserName(dynamicEntity.getUserName());
                msgEntity.setFromUserHeadImg(dynamicEntity.getUserHeadImg());
                iAppDynamicMsgService.insertAppDynamicMsg(msgEntity);
            }
        }

        /*
         * 添加好友的时间轴
         */
        // 获取可见动态好友id
        AppUserRelation appUserRelation = new AppUserRelation();
        appUserRelation.setUserId(dynamicEntity.getUserId());
        appUserRelation.setSelfState(0);
        appUserRelation.setSelfIsBeLooked(0);
        appUserRelation.setOtherState(0);
        appUserRelation.setOtherIsLook(0);
        // 动态的可见权限
        if (dynamicEntity.getSeeState() == 3 && StringUtils.isNotBlank(dynamicEntity.getRefuseUserIds())) {
            // 部分不可见
            List friendIds = Arrays.asList(dynamicEntity.getRefuseUserIds().split(","));
            appUserRelation.setUnfriendIds(friendIds);
        } else if (dynamicEntity.getSeeState() == 2 && StringUtils.isNotBlank(dynamicEntity.getSeeUserIds())) {
            // 部分人可见
            List friendIds = Arrays.asList(dynamicEntity.getRefuseUserIds().split(","));
            appUserRelation.setFriendIds(friendIds);
        } else if (dynamicEntity.getSeeState() == 1) { // 私密
            return AjaxResult.ok(0, "", null);
        }

        List<Long> friendIdList = iAppUserRelationService.selectAppUserIds(appUserRelation);
        // 插入好友时间轴
        if (friendIdList.size() > 0) {
            AppDynamicTimeline timelineEntity;
            for (Long friendId : friendIdList) {
                timelineEntity = new AppDynamicTimeline();
                timelineEntity.setCrtTime(dynamicEntity.getCrtTime());
                timelineEntity.setUserId(friendId);
                timelineEntity.setDynamicId(dynamicEntity.getId());
                timelineEntity.setIsOwn(0);
                timelineEntity.setIsRead(0);
                iAppDynamicTimelineService.insertAppDynamicTimeline(timelineEntity);
            }

        }

        return AjaxResult.ok(0, "发布动态成功", null);
    }

    @Override
    public void addLikeCount(Long dynamicId) {
        appDynamicMapper.addLikeCount(dynamicId);
    }

    @Override
    public void addCommentCount(Long dynamicId) {
        appDynamicMapper.addCommentCount(dynamicId);
    }

    @Override
    public void reduceCommentCount(Long dynamicId, int count) {
        appDynamicMapper.reduceCommentCount(dynamicId, count);
    }
}
